package com.zoke.support.base;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

import com.zoke.support.R;

/**
 * 网页界面的基类
 * 
 * @author JackWu
 * 
 */
public abstract class BaseWebActivity extends BaseActivity {
	protected RelativeLayout mBackContainer;
	private View mLoadingView;
	protected WebView mWebView;
	public static final String WEB_URL = "url";

	/** 默认支持 JavaScript **/
	protected boolean mEnableJs = true;
	/** 默认不显示竖向的滑动bar **/
	protected boolean mEnableVerScrollBar = false;
	/** 默认不显示横向的滑动bar **/
	protected boolean mEnableHorScrollBar = false;
//	private ReboundScrollView mContainerSw;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(getLayoutRes());
		mLoadingView = findViewById(R.id.baseweb_loading_indicator);
		mWebView = (WebView) findViewById(R.id.baseweb_webview);
		mWebView.getSettings().setJavaScriptEnabled(mEnableJs);
		mWebView.setVerticalScrollBarEnabled(mEnableVerScrollBar);
		mWebView.setHorizontalScrollBarEnabled(mEnableHorScrollBar);
//		mContainerSw = (ReboundScrollView) findViewById(R.id.scroll_mainlayout);
		// 支持侧滑删除
		mEnableSwipeRight = true;
		String url = getIntent().getStringExtra(WEB_URL);
		mWebView.loadUrl(url);
		initEvents();
	}

	protected void initEvents() {
		mWebView.setWebChromeClient(new WebChromeClient() {

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				super.onProgressChanged(view, newProgress);
			}

		});
		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				mWebView.loadUrl(url);
				return true;
			}

			@Override
			public void onReceivedSslError(WebView view,
					SslErrorHandler handler, android.net.http.SslError error) {
				handler.proceed();
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				showProgress();
			}

			private void showProgress() {
				mWebView.setVisibility(View.GONE);
				mLoadingView.setVisibility(View.VISIBLE);
			}

			private void dismissProgress() {
				mWebView.setVisibility(View.VISIBLE);
				mLoadingView.setVisibility(View.GONE);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				dismissProgress();
				onWebEnd(view, url);
			}
		});
	}

	protected abstract int getLayoutRes();

	/** 网页加载结束的时候调用 **/
	protected void onWebEnd(WebView view, String url) {

	}

}
